package opd.db.helpers;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DataSourceFromJNDI implements DataSource {

	private DataSource ds;
	private String jndiName;

	// ----------------------------------------------------------------------------------------------------
	
	public void setJndiName(String jndiName) throws NamingException {
		this.jndiName = jndiName;
		Context ctx = new InitialContext();
		ds = (DataSource) ctx.lookup(jndiName);
	}
	
	// ----------------------------------------------------------------------------------------------------
	
	public String getJndiName() {
		return jndiName;
	}
	
	// ----------------------------------------------------------------------------------------------------
	
	@Override
	public PrintWriter getLogWriter() throws SQLException {
		return ds.getLogWriter();
	}
	
	// ----------------------------------------------------------------------------------------------------

	@Override
	public int getLoginTimeout() throws SQLException {
		return ds.getLoginTimeout();
	}
	
	// ----------------------------------------------------------------------------------------------------

	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		return ds.getParentLogger();
	}

	// ----------------------------------------------------------------------------------------------------
	
	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		ds.setLogWriter(out);
	}

	// ----------------------------------------------------------------------------------------------------
	
	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		ds.setLoginTimeout(seconds);
	}

	// ----------------------------------------------------------------------------------------------------
	
	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return ds.isWrapperFor(iface);
	}

	// ----------------------------------------------------------------------------------------------------
	
	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		return ds.unwrap(iface);
	}

	// ----------------------------------------------------------------------------------------------------
	
	@Override
	public Connection getConnection() throws SQLException {
		return ds.getConnection();
	}

	// ----------------------------------------------------------------------------------------------------
	
	@Override
	public Connection getConnection(String username, String password) throws SQLException {
		return ds.getConnection(username, password);
	}

}
